package by.gravity.grsu.shedule.provider;

import java.util.Date;

import android.content.Context;
import by.gravity.common.task.CommonGsonAsyncTask;
import by.gravity.common.utils.StringUtil;
import by.gravity.grsu.shedule.api.GrsuSheduleAPI;
import by.gravity.grsu.shedule.model.Week;
import by.gravity.grsu.shedule.preference.PreferenceHelper;
import by.gravity.grsu.shedule.provider.listener.OnLoadStringArrayListener;
import by.gravity.grsu.shedule.provider.listener.OnLoadWeekListener;

public class SheduleProvider {

	private Context mContext;

	public SheduleProvider(Context context) {
		this.mContext = context;
	}

	public void getSpecialityList(final OnLoadStringArrayListener loadListener) {
		if (loadListener == null) {
			return;
		}

		new CommonGsonAsyncTask<String[]>(mContext) {

			@Override
			protected void onSuccess(String[] result) {
				if (result != null) {
					loadListener.onComplete(result);
				} else {
					loadListener.onError();
				}
			}
		}.execute(GrsuSheduleAPI.getSpeciality());
	}

	public void getKursList(final String speciality, final OnLoadStringArrayListener loadListener) {
		if (loadListener == null) {
			return;
		}

		new CommonGsonAsyncTask<String[]>(mContext) {

			@Override
			protected void onSuccess(String[] result) {
				if (result != null) {
					loadListener.onComplete(result);
				} else {
					loadListener.onError();
				}
			}
		}.execute(GrsuSheduleAPI.getKurs(speciality));
	}

	public void getGroupList(final String speciality, final String kurs, final OnLoadStringArrayListener loadListener) {
		if (loadListener == null) {
			return;
		}

		new CommonGsonAsyncTask<String[]>(mContext) {

			@Override
			protected void onSuccess(String[] result) {
				if (result != null) {
					loadListener.onComplete(result);
				} else {
					loadListener.onError();
				}

			}
		}.execute(GrsuSheduleAPI.getGroups(speciality, kurs));
	}

	public void getWeekList(final Date startWeekDate, final OnLoadWeekListener loadListener) {
		if (loadListener == null || startWeekDate == null) {
			return;
		}
		String speciality = PreferenceHelper.getSpeciality();
		String kurs = PreferenceHelper.getKurs();
		String group = PreferenceHelper.getGroup();
		if (StringUtil.isEmpty(speciality) || StringUtil.isEmpty(kurs) || StringUtil.isEmpty(group)) {
			loadListener.onError("");
			return;
		}

		new CommonGsonAsyncTask<Week>(mContext) {

			@Override
			protected void onSuccess(Week result) {
				if (result != null) {
					loadListener.onComplete(result);
				} else {
					loadListener.onError("");
				}
			}
		}.execute(GrsuSheduleAPI.getWeekShedule(speciality, kurs, group, startWeekDate.getTime()));
	}
}
